package common.window
{
	import drongo.desktop.window.DGNativeWindow;
	
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	import fairygui.GComponent;
	import fairygui.GRoot;
	import fairygui.RelationType;
	import fairygui.UIPackage;
	
	import robotlegs.bender.bundles.mvcs.MVCSBundle;
	import robotlegs.bender.extensions.contextView.ContextView;
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
	import robotlegs.bender.framework.api.IContext;
	import robotlegs.bender.framework.api.LogLevel;
	import robotlegs.bender.framework.impl.Context;

	public class FGUINativeWindow extends DGNativeWindow
	{
		/**
		 * 所属Content
		 */
		public var content:IContext;
		
		private var fairUI:GComponent;
		private var _inited:Boolean;
		public function FGUINativeWindow(title:String,width:Number=600,heigth:Number=400,openTransparent:Boolean=false,resizable:Boolean=true,maximizable:Boolean=true,minimizable:Boolean=true)
		{
			super(title,width,heigth,openTransparent,resizable,maximizable,minimizable);
		}
		/**
		 * 创建子内容
		 */		
		protected function init():void
		{
			if(_inited){
				return;
			}
			this.InitMVC();
			createChildren();
			createFairUI(pkgName,resName,userClass);
			_inited=true;
		}
		
		protected function InitMVC():void{
			this.content = new Context();
			//设置LOG级别
			this.content.logLevel=LogLevel.ERROR;
			this.content.install(MVCSBundle);
			this.content.configure(configure);
			this.content.configure(new ContextView(this.stage));
		}
		
		/**
		 * 子类可以选择重写来创建子内容 
		 */		
		protected function createChildren():void{
			
		}
		
		protected function get pkgName():String{
			throw new Error("子类必须重写");
			return null;
		}
		protected function get resName():String{
			throw new Error("子类必须重写");
			return null;
		}
		protected function get userClass():Object{
			return null;
		}
		
		protected function get configure():Class{
			return null;
		}
		
		/**
		 * 创建FairUI 
		 * @param pkgName
		 * @param resName
		 * @param userClass
		 */		
		public function createFairUI(pkgName:String,resName:String,userClass:Object=null):void{
			stage.frameRate = 60;
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.color = 0xFFFFFF;
			
			_groot=new GRoot();			
			stage.addChild(groot.displayObject);
			
			fairUI = UIPackage.createObject(pkgName,resName,userClass) as GComponent;
			fairUI.data=data;
			_groot.addChild(fairUI);
			//设置跟随大小变化
			fairUI.addRelation(_groot, RelationType.Size);
			//设置视图大小，如果不设置，窗口将显示不完全
			fairUI.setSize(_groot.width,_groot.height);
			
			mediatorMap.mediate(fairUI);
		}
		override public function set data(value:Object):void{
			super.data=value;
			if(fairUI){
				fairUI.data=data;
			}
		}
		override public function close():void{
			super.close();
			mediatorMap.unmediate(fairUI);
			fairUI.dispose();
			fairUI=null;
			this.content.destroy();
		}
		override protected function nativeWindow_activateHandler(e:Event):void{
			//激活的时候才创建，因为stage 只有激活后才能访问
			init();
		}
		override protected function window_resizeHandler(e:Event):void{
			
		}
		private var _groot:GRoot;
		public function get groot():GRoot{
			return _groot;
		}
		protected function get mediatorMap():IMediatorMap{
			return this.content.injector.getInstance(IMediatorMap);
		}
	}
}